<?php
##################### Auto Atjungimas nuo sistemos ########################
if (isset($_SESSION['username']) && isset($_SESSION['password'])) {
	$linfo = mysql_fetch_assoc(mysql_query1("SELECT levelis FROM users WHERE nick=" . escape($_SESSION['username']) ." AND pass=" . escape($_SESSION['password']) ." LIMIT 1"));
	if (!empty($linfo['levelis'])) { define("LEVEL",$linfo['levelis']); } // Priskiriam leveli = vartotojo lygi
	else {
		unset($_SESSION['username'],$_SESSION['password'],$_SESSION['id'],$_SESSION['lankesi']);	// Isvalom sesija
		session_unset(); session_destroy();	// Sunaikinam sesija
	}
	unset($linfo);
}

##################### Jei svetaine uzdaryta remontui ir jei jungiasi ne administratorius ###
if ($conf['Palaikymas'] == 1) {
	if (!defined("LEVEL") || LEVEL < 30) { redirect("remontas.php"); }
}

##################### Prisijungimas prie sistemos ########################
if (isset($_POST['action']) && $_POST['action'] == 'prisijungimas') {
	$strUsername = htmlspecialchars($_POST['vartotojas']);	// Vartotojo vardas
	$strPassword = md5($_POST['slaptazodis']);				// Slaptazodis
	$linfo = mysql_fetch_assoc(mysql_query1("SELECT id, login_data, levelis FROM users WHERE nick=" . escape($strUsername) ." AND pass='" . $strPassword ."'"));
	if (!empty($linfo)) {
    		$_SESSION['username'] = $strUsername;
    		$_SESSION['password'] = $strPassword;
    		$_SESSION['id'] = $linfo['id'];
    		$_SESSION['lankesi'] = $linfo['login_data'];
      		if (!defined("LEVEL")) { define("LEVEL", $linfo['levelis']); }
      		mysql_query1("UPDATE `users` SET `login_data` = NOW(), `ip` = INET_ATON(".escape($test['ip']).") WHERE `users`.`id` ='" . $linfo['id'] ."' LIMIT 1");
 	}
  	else { $strError = 'Klaidingi duomenys'; }	// Klaidos tekstas
  	unset($linfo,$strUsername,$strPassword);
}

####################### Atvaizduojam panele ###########################
if (!defined("LEVEL")) {
	$text = "
	<center>
		<form id=\"user_reg\" name=\"user_reg\" method=\"post\" action=\"\">
			<label for=\"vartotojas\">Vartotojas:</label><br/>
			<input name=\"vartotojas\" id=\"vartotojas\" type=\"text\"  value=\"\" maxlength=\"50\" /><br/>
			<label for=\"slaptazodis\">Slaptažodis:</label><br/>
			<input name=\"slaptazodis\" id=\"slaptazodis\" type=\"password\" value=\"\" maxlength=\"50\" /><br/>
			<input type=\"submit\" name=\"Submit\" value=\"Prisijungti\" />
			<input type=\"hidden\" name=\"action\" value=\"prisijungimas\" />
		</form>
	</center>";
	if ($conf['Registracija'] == 1) { $text .= "<a href=\"?id,41\">Registracija</a> "; }
	$text .= "<a href=\"?id,42\">Pamiršau</a>";
	if (isset($strError) && !empty($strError)) { klaida("Klaida",$strError); }
	lentele("Vartotojams",$text,"Prisijungimas:");
	unset($text,$strError);	// Apvalom
}
else {
	if (defined("LEVEL")  && LEVEL == 30) { $extra = "<font color=\"red\"><b>@</b></font>";}
	if (defined("LEVEL")  && LEVEL > 10 && LEVEL < 20) { $extra = "<font color=\"green\"><b>+</b></font>"; }
	else { $extra = "<font color=\"silver\"><b>%</b></font>"; }
	$pm = mysql_fetch_assoc(mysql_query1("SELECT count(id) AS `total` FROM `private_msg` WHERE `to`=".escape($_SESSION['username'])." AND `read`='NO'"));
	if ($pm['total'] != 0) { $img = "<blink><img src='images/icons/email_error.gif' alt='new' border='0' style=\"vertical-align: middle;\"/></blink>"; }
	else { $img = "<img src='images/icons/email.png' alt='@' border='0' style=\"vertical-align: middle;\"/>"; }
	
	$text = "
	<a href='?id,47;m,".$_SESSION['id']."'>".$extra." ".$_SESSION['username']."</a><br />
	<a href=\"?id,44\"><img src='images/icons/user_edit.png' alt='@' border='0' style=\"vertical-align: middle;\"/> Redaguoti profilį</a><br />
	<a href=\"?id,45\">$img Asmeninės žinutės</a><br />
	<a href=\"?id,43\"><img src='images/icons/key_go.png' alt='@' border='0' style=\"vertical-align: middle;\"/> Atsijungti</a><br />
	<span id=\"countdown1\" class=\"comment\">".date('Y-m-d H:i:s',(time() + ini_get('session.gc_maxlifetime')))." GMT+02:00</span><br/>
	";
	
	if (defined("LEVEL") && LEVEL == 30) {
		$text .= "\t<a href=\"?id,46\"><img src='images/icons/server_key.png' alt='@' border='0' style=\"vertical-align: middle;\"/> Administravimas</a><br />\n";
		$text .= "\t<a href=\"?id,48\"><img src='images/icons/folder.png' alt='@' border='0' style=\"vertical-align: middle;\"/> Funkcijų testai</a><br />\n";
		$text .= "\t<a href=\"?id,66\"><img src='images/icons/folder.png' alt='@' border='0' style=\"vertical-align: middle;\"/> Naujas forumas</a><br />\n";
	}
	lentele("Vartotojo panelė",$text,"Informacija");	//Piesiam paruosta lenta
	unset($text,$img,$pm);	// Isvalom tai ko nebenaudosim
}
?>
